//------------------------------------------------------------------------------
// <copyright file="App.xaml.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using DAnton.Utilities.Extensions;
using DotBeer.UI.WPFClient.ViewModels;
using DotBeer.UI.WPFClient.Views;
using System.Diagnostics;
using DotBeer.Business.Components.Exceptions;
using DotBeer.Data.Properties;

namespace DotBeer.UI.WPFClient
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.UpgradeSettingsIfNeeded();

            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");

            ApplicationExceptionHandler.ExceptionOccured += new EventHandler<DotBeer.Business.Entities.CustomEventArgs.ApplicationExceptionEventArgs>(ApplicationExceptionHandler_ExceptionOccured);

            WindowMain main = new WindowMain();
            main.DataContext = new MainWindowViewModel();
            main.Show();

            this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
        }

        private void ApplicationExceptionHandler_ExceptionOccured(object sender, DotBeer.Business.Entities.CustomEventArgs.ApplicationExceptionEventArgs e)
        {
            AppTextExceptionFormatter formatter = new AppTextExceptionFormatter();
            CustomMessageBox.Show(e.Message, "Error");
        }

        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            this.HandleException(e.Exception);
            e.Handled = true;
        }

        private void HandleException(Exception ex)
        {
            WindowExceptionReport windowError = new WindowExceptionReport(ex);
            windowError.SetOwner();
            windowError.ShowDialog();

            if (windowError.DialogResult != null)
            {
                if ((bool)windowError.DialogResult)
                {
                    Process.Start(Application.ResourceAssembly.Location);
                    this.Shutdown();
                }
            }
        }

        private void UpgradeSettingsIfNeeded()
        {
            if (UpdateSettings.Default.UpgradeSettings)
            {
                UpdateSettings.Default.UpgradeSettings = false;
                UpdateSettings.Default.Save();

                CopyItemBuildSettings.Default.Upgrade();
                CopyItemBuildSettings.Default.Save();

                CopySkillBuildSettings.Default.Upgrade();
                CopySkillBuildSettings.Default.Save();

                DisplayOptionsSettings.Default.Upgrade();
                DisplayOptionsSettings.Default.Save();

                ItemsCollectionControlSettings.Default.Upgrade();
                ItemsCollectionControlSettings.Default.Save();

                WindowMainSettings.Default.Upgrade();
                WindowMainSettings.Default.Save();

                WindowsBindingsSettings.Default.Upgrade();
                WindowsBindingsSettings.Default.Save();

                WindowsPositionSettings.Default.Upgrade();
                WindowsPositionSettings.Default.Save();
            }
        }
    }
}
